home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 419 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.0 KB  |  58 lines

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: clamage@Eng.sun.com (Steve Clamage)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Explicit?  What's that?
  5. Date: 20 Feb 1996 18:39:42 GMT
  6. Organization: Sun Microsystems Inc.
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4gd4b9$c43@engnews1.Eng.Sun.COM>
  9. References: <4gb00j$2ne@charnel.ecst.csuchico.edu>
  10. Reply-To: clamage@Eng.sun.com
  11. NNTP-Posting-Host: taumet.eng.sun.com
  12. Content-Type: text
  13. X-Nntp-Posting-Host: taumet.eng.sun.com
  14. Content-Length: 1149
  15. X-Lines: 34
  16. Originator: clamage@taumet
  17.  
  18. In article 2ne@charnel.ecst.csuchico.edu, mcelroy@ecst.CSUChico.EDU (James Robert McElroy) writes:
  19. >Borland sent me an upgrade offer for C++ V. 5.0.  Among
  20. >the new features they say they support is "Explicit".
  21. >No mention of what "Explicit" means is given.
  22. >
  23. >What is this critter?
  24.  
  25. A new language feature that lets you specify that a constructor is not
  26. to be used for implicit type conversions. It has a meaning only for
  27. constructors that can take a single argument. Example:
  28.  
  29.     class String {
  30.     public:
  31.         String();
  32.         String(const char*);
  33.         explicit String(int); // establish minimum size
  34.     };
  35.     String operator+(const String&, const String&);
  36.  
  37.     String s("Hello");
  38.     String t = s + " World"; // OK, implicitly converts char* to String
  39.     t = s + 2; // ERROR
  40.     t = s + String(2); // OK, convert int to String explicitly
  41.  
  42. Without the "explicit", the expression s+2 would silently create a String
  43. of capacity 2 and append it to s. Probably that isn't what the programmer
  44. had in mind. With "explicit", the implicit conversion doesn't occur.
  45.  
  46. The last line is OK because the constructor is invoked explicitly.
  47.  
  48. ---
  49. Steve Clamage, stephen.clamage@eng.sun.com
  50.  
  51.  
  52. [ To submit articles: Try just posting with your newsreader.
  53.               If that fails, use mailto:std-c++@ncar.ucar.edu
  54.   FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  55.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  56.   Comments? mailto:std-c++-request@ncar.ucar.edu
  57. ]
  58.